<?php
class Anews {
	public static function main($funcode) {
		sview::verify(true, true);
		switch ($funcode) {
			case 1:
				self::__dolist();
				break;
			case 2:
				self::__dostatus();
				break;
			case 3:
				self::__dounread();
				break;
			default:
				sview::$view->assign_error(404, lang::text(404));
		}
	}
	
	//消息列表3-2-1
	private static function __dolist() {
		//默认的系统类别数据信息
		$data = array('datasets'=>array());
		$rtype   = intval(sview::$ctrl['type']);
		$start   = intval(sview::$ctrl['offset']);
		$limit   = intval(sview::$ctrl['limit']);
		$userid  = acl::getinst('vedio')->user['id'];
		$setting = model::load('live/usersetting')->set_devision($userid)->get_one($userid);
		$user    = model::load('live/userinfo')->set_devision($userid)->get_one($userid);
		$dtime   = $stime = $rtime = $user['regtime'];
		if ($setting) {
			$rtime = ($setting['allread'] > $user['regtime']) ? $setting['allread'] : $user['regtime'];
			$dtime = ($setting['truncate'] > $user['regtime']) ? $setting['truncate'] : $user['regtime'];
			$stime = ($rtime > $dtime)? $rtime:$dtime;
		}
		//获取已经被删除的msgids
		$where   = array('userid'=>$userid, 'status'=>2, 'stime'=>array($dtime, orm::OP_GT));
		$dmsgids = model::load('live/msgstatus')->set_devision($userid)->get_only_column(0, -1, $where, 'msgid');
		if (!is_array($dmsgids)) {
			$dmsgids = array();
		}
		//获取已经被读取的msgids
		$where   = array('userid'=>$userid, 'status'=>1, 'stime'=>array($rtime, orm::OP_GT));
		$rmsgids = model::load('live/msgstatus')->set_devision($userid)->get_only_column(0, -1, $where, 'msgid');
		if (!is_array($rmsgids)) {
			$rmsgids = array();
		}
		//合并两个数组资料信息
		$msgids  = array_merge($dmsgids, $rmsgids);
		if ($limit < 1 || $limit > 30) {
			$limit = 4;
		}
		if ($rtype == 1) { //系统消息
			$where = self::__where(1, $userid, $dmsgids, $dtime);
			$data  = self::__list($start, $limit, $where, $rmsgids, $rtime);
		} else if ($rtype == 2) { //活动通知
			$where = self::__where(2, $userid, $dmsgids, $dtime);
			$data  = self::__list($start, $limit, $where, $rmsgids, $rtime);
		} else {
			$where = self::__where(0, $userid, $dmsgids, $dtime);
			$data  = self::__list($start, $limit, $where, $rmsgids, $rtime);
		}
		sview::$view->assign('datasets', $data);
		sview::$view->display();
	}
	
	//获取条件限制的资源整合
	private static function __where($dictid, $userid, $dmesgids, $dtime) {
		$where = array(array('status', 1, orm::OP_EQ),
				array('stime', $dtime, orm::OP_GE), array('reveiverid', array('0', $userid), orm::OP_IN));
		if ($dictid) {
			$where[] = array('dictid', $dictid, orm::OP_EQ);
		}
		if (is_array($dmesgids) && !empty($dmesgids)) { //排除被删除的记录
			$where[] = array('id', $dmesgids, orm::OP_NOTIN);
		}
		return $where;
	}
	
	//获取系统消息的总数
	private static function __stats($dictid, $userid, $msgids, $stime) {
		$where   = array('status'=>1, 'stime'=>array($stime, orm::OP_GE),
			'reveiverid'=>array(array('0', $userid), orm::OP_IN));
		if (is_array($msgids) && !empty($msgids)) { //排除被删除和已经阅读的记录
			$where['id'] = array($msgids, orm::OP_NOTIN);
		}
		if ($dictid != -1) {
			$where['dictid'] = $dictid;
		}
		$total = model::load('live/msg')->get_total($where);
		return $total;
	}
	
	//获取资料信息列表
	private static function __list($start, $limit, $where, $rmsgids, $rtime) {
		$field = 'id,title,msg,reftype,refid as userid,stime';
		$list  = model::load('live/msg')->get_only_list($start, $limit, $where, $field, 'id', orm::DESC);
		if ($list) {
			$userid = acl::getinst('vedio')->user['id'];
			$uorm   = model::load('live/userinfo');
			$aorm   = model::load('live/anchor');
			$sorm   = model::load('live/studio');
			foreach ($list as &$item) {
				$item['flag'] = (($item['stime'] < $rtime) || in_array($item['id'], $rmsgids))? 1:0;
				if ($item['userid'] > 0 && $item['userid'] != $userid) {
					$user   = $uorm->set_devision($item['userid'])->get_one($item['userid']);
					$item['nickname'] = htmlspecialchars_decode($user['nickname']);
					$item['avtar']    = $user['thumb'];
					$anchor = $aorm->get_one($item['userid']);
					$item['roler']    = $anchor ? 1 : 0;
					$field  = array('userid'=>$item['userid']);
					$studio = $sorm->get_item($field);
					$item['studioid'] = $studio ? $studio['id'] : 0;
					$item['userid']   = (int)$item['userid'];
				}
				$item['title'] = htmlspecialchars_decode($item['title']);
				$item['msg'] = stripcslashes($item['msg']);
				$item['msg'] = htmlspecialchars_decode($item['msg']);
				$item['msg'] = str_replace("&nbsp;", " ", $item['msg']);
				$item['msg'] = strip_tags($item['msg']);
			}
		} else {
			$list = array();
		}
		return $list;
	}
	
	//删除/设置已读消息3-2-2
	private static function __dostatus() {
		$userid = acl::getinst('vedio')->user['id'];
		$mids   = trim(sview::$ctrl['ids']);
		$status = trim(sview::$ctrl['status']);
		$mids   = explode(',', $mids);
		$mids   = array_filter($mids, 'is_numeric');
		//遍历要处理的消息id列表
		if ($mids) {
			foreach ($mids as $mid) {
				$msg    = model::load('live/msg')->get_one($mid);
				if ($msg['status'] != 1 || ($msg['reveiverid'] != 0 && $msg['reveiverid'] != $userid)) {
					continue;
				}
				$where  = array('userid'=>$userid, 'msgid'=>$mid);
				$orm    = model::load('live/msgstatus')->set_devision($userid);
				$mstatus= $orm->get_item($where);
				if ($mstatus) {
					$orm->save($mstatus['id'], array('status'=>$status));
				} else {
					$fields = array('userid'=>$userid, 'msgid'=>$mid, 'status'=>$status, 'stime'=>config::$nowtime);
					$orm->new_one($fields);
				}
			}
		}
		sview::$view->display();
	}
	
	//获取未读的数3-2-3
	private static function __dounread() {
		$userid  = acl::getinst('vedio')->user['id'];
		$dtime   = $stime = $rtime = 0;
		$setting = model::load('live/usersetting')->set_devision($userid)->get_one($userid);
		if ($setting) {
			$user  = model::load('live/userinfo')->set_devision($userid)->get_one($userid);
			$rtime = ($setting['allread'] > $user['regtime']) ? $setting['allread'] : $user['regtime'];
			$dtime = ($setting['truncate'] > $user['regtime']) ? $setting['truncate'] : $user['regtime'];
			$stime = ($rtime > $dtime)? $rtime:$dtime;
		}
		//获取已经被删除的msgids
		$where   = array('userid'=>$userid, 'status'=>2, 'stime'=>array($dtime, orm::OP_GT));
		$dmsgids = model::load('live/msgstatus')->set_devision($userid)->get_only_column(0, -1, $where, 'msgid');
		if (!is_array($dmsgids)) {
			$dmsgids = array();
		}
		//获取已经被读取的msgids
		$where   = array('userid'=>$userid, 'status'=>1, 'stime'=>array($rtime, orm::OP_GT));
		$rmsgids = model::load('live/msgstatus')->set_devision($userid)->get_only_column(0, -1, $where, 'msgid');
		if (!is_array($rmsgids)) {
			$rmsgids = array();
		}
		//合并两个数组资料信息
		$msgids  = array_merge($dmsgids, $rmsgids);
		$data['notication'] = self::__stats(1, $userid, $msgids, $stime);
		$data['event']      = self::__stats(2, $userid, $msgids, $stime);
		sview::$view->assign('datasets', $data);
		sview::$view->display();
	}
}